import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'camera_product_plugin_platform_interface.dart';

/// An implementation of [CameraProductPluginPlatform] that uses method channels.
class MethodChannelCameraProductPlugin extends CameraProductPluginPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('camera_product_plugin');

  final eventChannel = const EventChannel('camera_product_plugin_event');

  /// 订阅 - 解决  eventChannel 重复订阅的问题
  StreamSubscription? _subscription;

  /// 监听
  void Function(dynamic event)? onData;

  void cancelListening() {
    // debugPrint("===== 订阅取消 ==== ");
    _subscription?.cancel();
    _subscription = null;
  }

  void onListening(void Function(dynamic event) onData) {
    _subscription?.cancel();
    _subscription = eventChannel.receiveBroadcastStream().listen((event) async {
      if (event is! Map) {
        print('------- camera_product_plugin_method_channel -----------');
        print(event);
        print("event 类型有误，当前类型: ${event.runtimeType}");
        return;
      }
      // 执行回调
      onData(event);
    });
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version =
        await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<String?> initCameraSDK() async {
    final initSDK = await methodChannel.invokeMethod("initCameraSDK");
    return initSDK;
  }

  ///扫描蓝牙
  @override
  Future<String?> scanBluetooth() async {
    final device = await methodChannel.invokeMethod("scanBluetooth");
    return device;
  }

  ///连接蓝牙
  @override
  Future<String?> connectBluetooth(String deviceJson) async {
    final state = await methodChannel
        .invokeMethod("connectBluetooth", {"param": deviceJson});
    return state;
  }

  ///扫描WIFI
  @override
  Future<String?> scanWIFI() async {
    final wifi = await methodChannel.invokeMethod("scanWIFI");
    return wifi;
  }

  ///连接WIFI
  @override
  Future<String?> connectWIFI(String wifiJson) async {
    final connectWIFI =
        await methodChannel.invokeMethod("connectWIFI", {"param": wifiJson});
    return connectWIFI;
  }

  ///拍照
  @override
  Future<String?> takePhoto() async {
    final photo = await methodChannel.invokeMethod("takePhoto");
    return photo;
  }

  ///移除相机监听
  @override
  Future<String?> removeCameraListener() async {
    final listener = await methodChannel.invokeMethod("removeCameraListener");
    //取消事件监听
    cancelListening();
    return listener;
  }

  ///断开蓝牙设备
  @override
  Future<String?> disConnectBluetooth() async {
    final status = await methodChannel.invokeMethod("disConnectBluetooth");
    return status;
  }

  ///获取当前已连接设备
  @override
  Future<String?> getCurrentDevice() async {
    final deviceName = await methodChannel.invokeMethod("getCurrentDeviceName");
    return deviceName;
  }

///获取当前已连接设备SSID
  @override
  Future<String?> getCurrentDeviceSSID() async {
    final ssid = await methodChannel.invokeMethod("getCurrentDeviceWifiSSID");
    return ssid;
  }

  ///获取当前已连接设备WIFI状态
  @override
  Future<String?> getCurrentDeviceWifiSatate() async {
    final ssid = await methodChannel.invokeMethod("getCurrentDeviceWifiSatate");
    return ssid;
  }

}
